package ru.nsu.fit.iconveyor.launcher;

import ru.nsu.fit.iconveyor.conveyor.Conveyor;
import ru.nsu.fit.iconveyor.conveyor.storage.importer.Importer;
import ru.nsu.fit.iconveyor.engine.Engine;
import ru.nsu.fit.iconveyor.engine.smart.SmartEngine;
import ru.nsu.fit.iconveyor.log.console.ConsoleLoggingConveyorExecutionObserver;
import ru.nsu.fit.iconveyor.log.console.ConsoleLoggingEngineStateObserver;

public class Launcher {

    private static final int DEFAULT_THREAD_NUMBER = 2;

    public static void main(String [] args) {
        if(args.length < 1) {
            System.err.println("conveyor file not specified");
            return;
        }

        final String conveyorFileName = args[0];

        Integer threadNumber = null;
        if(args.length >= 2) {
            try {
                threadNumber = Integer.parseInt(args[1]);
            } catch (NumberFormatException e) {

            }
        }
        if(threadNumber == null || threadNumber < 1) {
            threadNumber = DEFAULT_THREAD_NUMBER;
        }

        Engine engine = new SmartEngine(threadNumber);
        Conveyor conveyor;
        try {
            conveyor = Importer.importFromFile(conveyorFileName);
        } catch (Exception e) {
            System.err.println("error during importing conveyor: " + e.getLocalizedMessage());
            return;
        }

        engine.setConveyor(conveyor);

        engine.addConveyorExecutionObserver(new ConsoleLoggingConveyorExecutionObserver());
        engine.addEngineStateObserver(new ConsoleLoggingEngineStateObserver());

        engine.start();

    }

}
